package com.doform.android.xianggucheng.activity.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-27.
 * GitHub：https://github.com/cxydxpx
 */

public class RegisterActivity extends BaseTitleActivity {

    @OnClick({R.id.btn_next, R.id.iv_delete})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                beforeNext();
                break;
            case R.id.iv_delete:
                et_number.setText("");
                break;
            default:
                break;
        }
    }

    private void beforeNext() {

        String trim = et_number.getText().toString().trim();

        if (TextUtils.isEmpty(trim)) {
            TsUtils.showToast("请输入手机号");
            return;
//        } else if (RexUtils.isMobileNO(trim)) {
//            TsUtils.showToast("手机格式不正确");
//            return;
        }
        postPhoneToService(trim);

    }

    private void postPhoneToService(final String phone) {

        HttpManager.instance()
                .postCodeToService(new ReqRegister(phone))
                .subscribe(new ErrorHandleSubscriber<String>() {
                               @Override
                               public void onNext(@NonNull String s) {
                                   startActivity(new Intent(RegisterActivity.this, RegisterActivity2.class)
                                           .putExtra(Constans.EXTRA_REGISTER_PHONE, phone));
                               }
                           }
                );
    }

    @BindView(R.id.et_number)
    EditText et_number;

    @BindView(R.id.iv_delete)
    ImageView iv_delete;

    @Override
    protected int layoutResId() {
        return R.layout.activity_register;
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("注册");
    }
}
